//
//  UIViewController+Placeholder.m
//  WCImApp
//
//  Created by alfie on 2019/11/19.
//  Copyright © 2019 WCImApp. All rights reserved.
//

#import "UIViewController+Placeholder.h"
#import <objc/runtime.h>

@implementation UIViewController (MOPlaceholder)

static const char *MOPlaceholderKey = "MOPlaceholderKey";

- (void)setPlaceholder:(MOPlaceholder *)placeholder {
    objc_setAssociatedObject(self, MOPlaceholderKey, placeholder, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}



#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
- (MOPlaceholder *)placeholder {
    MOPlaceholder *_placeholder = objc_getAssociatedObject(self, MOPlaceholderKey);
    if (!_placeholder) {
        CGRect frame;
        if ([self.parentViewController isKindOfClass:[UINavigationController class]]) {
            frame = CGRectMake(0, WCI_IPHONE_NAVIGATIONBAR_HEIGHT, self.view.frame.size.width, self.view.frame.size.height - WCI_IPHONE_NAVIGATIONBAR_HEIGHT);
        } else {
            frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
        }
        __weak typeof(self) weakSelf = self;
        _placeholder = [[[[[MOPlaceholder new] makeTarget:self] makeFrame:frame] makeLoginHandler:^{
//            [weakSelf checkLogin];
        }] makeRefreshHandler:^{
            if ([weakSelf respondsToSelector:NSSelectorFromString(@"refreshData")]) {
                [weakSelf performSelector:NSSelectorFromString(@"refreshData")];
            } else if ([weakSelf respondsToSelector:NSSelectorFromString(@"requestData")]) {
                [weakSelf performSelector:NSSelectorFromString(@"requestData")];
            }
        }];
        objc_setAssociatedObject(self, MOPlaceholderKey, _placeholder, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return _placeholder;
}
#pragma clang diagnostic pop

@end
